<?php

class Quantri_NhanvienController extends Zend_Controller_Action
{

    public function init()
    {
        $option=array(
        		"layout" => "layout",
        		"layoutPath" => APPLICATION_PATH."/layouts/scripts/quantri"
        );
        Zend_Layout::startMvc($option);
        
        $this->view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
    }

    public function indexAction()
    {
    	$nhanvien = new Quantri_Model_Nhanvien();
    	$rsNhanvien = $nhanvien->GetAll();
    	$rsquyennhanvien = $nhanvien->GetQuyen();
    	
    	$this->view->rsquyennhanvien=$rsquyennhanvien;
    	$this->view->rsNhanvien = $rsNhanvien->toArray();
    }

    public function saveAction()
    {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);

        $nhanvien = new Quantri_Model_Nhanvien();
        
        $idNhanvien = $this->_request->getParam("idNhanvien", "-1");
        $hoten = $this->_request->getParam("hoten","");
        $ngaysinh = $this->_request->getParam("ngaysinh","");
        $diachi = $this->_request->getParam("diachi","");
        $dienthoai = $this->_request->getParam("dienthoai","");
        $email = $this->_request->getParam("email","");
        $trangthai = $this->_request->getParam("trangthai","off");
        $tendangnhap = $this->_request->getParam("tendangnhap","");
        $matkhau = $this->_request->getParam("matkhau","");
        $maquyen = $this->_request->getParam("maquyen","");
        
        if($trangthai=="on")
            $trangthai = 1;
        else
            $trangthai = 0;
        
        list($day, $month, $year) = split('[/.-]', $ngaysinh);
        
        $nhanvien->Id = $idNhanvien;
        $nhanvien->HoTen = $hoten;
        $nhanvien->NgaySinh = $year . '-' . $month . '-' . $day;
        $nhanvien->DiaChi = $diachi;
        $nhanvien->DienThoai = $dienthoai;
        $nhanvien->Email = $email;
        $nhanvien->Trangthai = $trangthai;
        $nhanvien->TenDangNhap = $tendangnhap;
        $nhanvien->MatKhau = $matkhau;
        $nhanvien->MaQuyen = $maquyen;
        
        $nhanvien->Save();
    }

    public function xoanhanvienAction()
    {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        $nhanvien = new Quantri_Model_Nhanvien();
        $id = $this->_request->getParam("id", "-1");
        
        $nhanvien->XoaNhanVien($id);
    }

    public function suaAction()
    {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        $idNhanvien = $this->_request->getParam("id","-1");
        
        $nhanvien = new Quantri_Model_Nhanvien();
        $nhanvien->Id = $idNhanvien;
        
        $rsNhanvien = $nhanvien->GetById();
        
        //var_dump($rsNhanvien[0]);
        echo json_encode($rsNhanvien);
    }

    public function updateTrangthaiAction()
    {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        $nhanvien = new Quantri_Model_Nhanvien();
        $id = $this->_request->getParam("id", "-1");
        $trangthai = $this->_request->getParam("id", "-1");
        
        if($trangthai==-1)
            return;
        
        $trangthai = ($trangthai==1) ? '0' : '1';
        
        $nhanvien->Trangthai = $trangthai;
        $nhanvien->UpdateTrangthai();
    }


}





